package one.bugu.android.demon.util;

import android.content.Context;

import java.util.List;

import one.bugu.android.demon.bean.MedalBean;
import one.bugu.android.demon.bean.UserMedalBean;
import one.bugu.android.demon.constant.Constant;

/**
 * PackageName: one.bugu.android.demon.util
 * Content: 头衔弹框判断处理
 *
 * @author Administrator
 * @date 2018/12/10 14:54
 */
public class MedalUtils {

    /**
     * 单例对象
     */
    private static volatile MedalUtils singleton = null;

    private MedalUtils() {

    }

    /**
     * 获取单例对象
     *
     * @return 单例对象
     */
    public static MedalUtils getInstance() {
        if (singleton == null) {
            synchronized (MedalUtils.class) {
                if (singleton == null) {
                    singleton = new MedalUtils();
                }
            }
        }
        return singleton;
    }

    /**
     * BGT头衔是否显示
     *
     * @param level
     * @return
     */
    public boolean isShowBgtMedalDialog(Context context, int level, boolean isBGT) {
        int userId = PreferencesUtil.getInstance().getInt(context, Constant.USER_ID);
        String medalLevel = PreferencesUtil.getInstance().getString(context, Constant.MEDAL_LEVEL);
        UserMedalBean medalData = JsonFormatUtil.parseJsonToBean(medalLevel, UserMedalBean.class);
        if (medalData != null) {
            //当前保存头衔等级
            int saveLev = isBGT ? medalData.getBgtLevel() : medalData.getCalLevel();
            return saveLev < level || userId != medalData.getUserId();
        }
        return true;
    }

    /**
     * 保存头衔等级
     *
     * @param context
     * @param level
     * @param isBGT
     */
    public void saveMedalLevStatus(Context context, int level, boolean isBGT) {
        int userId = PreferencesUtil.getInstance().getInt(context, Constant.USER_ID);
        String medalLevel = PreferencesUtil.getInstance().getString(context, Constant.MEDAL_LEVEL);
        UserMedalBean medalData = JsonFormatUtil.parseJsonToBean(medalLevel, UserMedalBean.class);
        if (medalData != null) {
            int saveLev = isBGT ? medalData.getBgtLevel() : medalData.getCalLevel();
            if (saveLev >= level && userId == medalData.getUserId()) {
                return;
            }
        }
        //保存数据
        UserMedalBean bean = new UserMedalBean();
        bean.setUserId(userId);
        if (isBGT) {
            bean.setBgtLevel(level);
            bean.setCalLevel(medalData == null ? 0 : medalData.getCalLevel());
        } else {
            bean.setBgtLevel(medalData == null ? 0 : medalData.getBgtLevel());
            bean.setCalLevel(level);
        }
        String jsonData = JsonFormatUtil.objConversionJsonString(bean);
        PreferencesUtil.getInstance().putString(context, Constant.MEDAL_LEVEL, jsonData);
    }

    /**
     * 获取算力头衔等级
     */
    public int getCalMedalLevel(Context context) {
        int result = 0;
        String medalLevel = PreferencesUtil.getInstance().getString(context, Constant.MEDAL_LEVEL);
        UserMedalBean medalData = JsonFormatUtil.parseJsonToBean(medalLevel, UserMedalBean.class);
        if (medalData != null) {
            result = medalData.getCalLevel();
        }
        return result;
    }

    /**
     * 已获取的最大头衔ID
     */
    public int getMaxRowId(List<Integer> data) {
        int max = -1;
        if (data != null && !data.isEmpty()) {
            max = data.get(0);
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i) > max) {
                    max = data.get(i);
                }
            }
        }
        return max;
    }

    /**
     * 获取最接近的ID
     *
     * @param data
     * @param rowId
     * @return
     */
    public int getNearByRowId(List<Integer> data, int rowId) {
        int nearByRowId = -1;
        for (int i = 0; i < data.size(); i++) {
            int id = data.get(i);
            if (rowId > id) {
                nearByRowId = id;
            }
        }
        return nearByRowId;
    }

    /**
     * 根据ID获取头衔等级
     *
     * @param rowId
     * @param powerTitle
     * @return
     */
    public int getMedalevel(int rowId, List<MedalBean> powerTitle) {
        int level = 0;
        if (powerTitle != null) {
            for (int i = 0; i < powerTitle.size(); i++) {
                if (powerTitle.get(i).getRowId() == rowId) {
                    level = i;
                    break;
                }
            }
        }
        return level;
    }
}
